﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using NHibernate.Cfg.MappingSchema;

namespace MVCGen.Generators.Entity {
	public class HbmManyToOnePropertyGenerator : ICodeTypeMemberGenerator {
		private HbmManyToOne manyToOne;

		public HbmManyToOnePropertyGenerator(HbmManyToOne manyToOne) {
			this.manyToOne = manyToOne;
		}

		public CodeTypeMember Generate() {
			CodeMemberProperty result = new CodeMemberProperty() {
				Name = manyToOne.name,
				Type = new CodeTypeReference(manyToOne.@class),
				Attributes = MemberAttributes.Public | MemberAttributes.VTableMask,
				HasGet = true,
				HasSet = true
			};
			result.GetStatements.Add(
				new CodeMethodReturnStatement(
					new CodeFieldReferenceExpression(
						new CodeThisReferenceExpression(), manyToOne.name.MakeFieldName()
					)
				)
			);
			result.SetStatements.Add(
				new CodeAssignStatement(
					new CodeFieldReferenceExpression(
						new CodeThisReferenceExpression(), manyToOne.name.MakeFieldName()
					), 
					new CodePropertySetValueReferenceExpression()
				)
			);
			return result;
		}
	}
}
